import {OrderOnline} from '@/base/model/OrderOnline';
import {getDealerList, postorderOnline} from '@/base/api/ServiceApi';
import {getDeviceSortV2} from '@/base/api/DeviceSortApi';
import {deepAssign} from '@/base/util/Objects';
import {getDeviceModel, getDeviceModelV2} from '@/base/api/DeviceModelApi';
import {Page} from '@/base/Page';
import {DeviceSort} from '@/base/model/DeviceSort';
import {DeviceModel} from '@/base/model/DeviceModel';

export class FormBloc {
  form= new OrderOnline();
  loading = false;
  fileList: Array<any> = [];
  fileList2: Array<any> = [];
  num: number = 0;
  sortList: any = [];
  modelList: any = [];
  dealerList: any = [];
  // eslint-disable-next-line standard/object-curly-even-spacing
  awards=[{ name: '', quantity: 1, probability: 0, imgUrl: '', isValue: 0 }];
  awards2=[{ name: '', quantity: 1, probability: 0, imgUrl: '', isValue: 0 }];
  isDetail = false;
  isinfo = '请先选择产品名称';
  type: string | undefined;
  // page: Page<DeviceSort> = new Page<DeviceSort>([], 0, 1, 15, 0, []);
  // page1: Page<DeviceModel> = new Page<DeviceModel>([], 0, 1, 15, 0, []);

  addOrderonline(form) {
    this.loading = true;
    return postorderOnline({
      sortId: form.sortId,
      modelId: form.modelId,
      quantity: form.Quantity,
      dealerName: form.dealerName,
      remarkOrderNumber: form.remarkOrderNumber,
      consigneeName: form.ConsigneeName,
      mobilePhone: form.MobilePhone,
      consigneeAddress: form.ConsigneeAddress,
      remark: form.remark
    }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }
  getAllSort() {
    this.loading = true;
    return getDeviceSortV2({
    }).then((res) => {
      // @ts-ignore
      let jsonArr = JSON.parse(res.data);
      if (jsonArr.length) {
        this.sortList = jsonArr.map(function (item) {
          var obj = { id: 0, sortName: '' };
          // @ts-ignore
          obj.id = item.id;
          obj.sortName = item.name;
          return obj;
        });
      }
      return this.sortList;
    }).finally(() => {
      this.loading = false;
    });
  }
  getAllModel(pageIndex: number, pageSize: number, sortId) {
    this.loading = true;
    return getDeviceModelV2({
      pageIndex,
      pageSize,
      id: sortId,
      sort: 1 // 销量排序
    }).then((res) => {
      // @ts-ignore
      let jsonArr2 = JSON.parse(res.data);
      // console.log(jsonArr2.items);
      // deepAssign(this.page1, res.data);
      if (jsonArr2.items.length) {
        this.modelList = jsonArr2.items.map(function (item) {
          var obj = { id: 0, modelName: '' };
          obj.id = item.id;
          obj.modelName = item.name;
          return obj;
        });
      }
      return this.modelList;
    }).finally(() => {
      this.loading = false;
    });
  }

  getAllDealer() {
    this.loading = true;
    return getDealerList({
      dealerId: 1
    }).then((res) => {
      // eslint-disable-next-line no-eval
      let data = eval('(' + res.data + ')');
      if (data.errCode === 200) {
        this.dealerList = data.errMsg;
      }
      // console.log(this.dealerList);
    }).finally(() => {
      this.loading = false;
    });
  }
  reset() {
    location.reload();
  }

}
